extends PopupPanel

onready var item_list = $MarginContainer/VBoxContainer/ScrollContainer/ItemList
# 回调方法，这个方法返回被选择的index和txt名称值
onready var search_key_edit = $MarginContainer/VBoxContainer/HBoxContainer/SearchKey
onready var title = $MarginContainer/VBoxContainer/Label
onready var content_scroll = $MarginContainer/VBoxContainer/ScrollContainer

var call_back:FuncRef = null

# 列表项数据，顺序是无法更改的
var items = []
# 被过滤出来的items,如果点击了搜索的话
# 这个列表是个map,前缀有index的映射关系
var filter_items = []

# 初始化就选择的下标，没有就是-1
var init_select_index = -1

# 加载过滤出来的列表数据？
var load_filter = false

# 当前列表被选择的数据
var item_list_select_index = -1

# 设置数据，可以设置默认选择的index
func set_data(title,list_data,call_back,_init_select_index_ = -1):
	self.title.text = title
	self.items = list_data
	self.call_back = call_back
	self.init_select_index = _init_select_index_
	self.item_list_select_index = _init_select_index_
	load_list()
	init_select_index = -1
	

func load_list():
	item_list.clear()
	if load_filter:
		for item in filter_items:
			item_list.add_item(item["value"])
	else:
		# 直接加载items的数据，这个列表的数据一定是一个顺序的切不可更改的
		for item in items:
			# 添加数据
			item_list.add_item(item)
		if init_select_index != -1:
			# 设置这条被选择
			var temp_index = init_select_index
			item_list.select(init_select_index)
			yield(get_tree().create_timer(0.1),"timeout")
			var max_v = item_list.get_v_scroll().max_value
			var v = temp_index / (items.size() * 1.0) * max_v
			var tween = get_tree().create_tween()
			tween.tween_property(content_scroll,"scroll_vertical",v,0.3)


func _on_Cancel_pressed():
	MainTool.getUILayer().pop_ui()


func _on_Ok_pressed():
	if item_list_select_index == -1: 
		MainTool.toast_info("你需要选择一条数据")
		return
	var return_index = item_list_select_index
	var return_value = ""
	if load_filter:
		return_index = filter_items[return_index]["index"]
	return_value = items[return_index]
	if call_back != null:
		call_back.call_func(return_index,return_value)
		
	MainTool.getUILayer().pop_ui()

# 搜索及过滤出合适的列表数据
func _on_SearchBtn_pressed():
	item_list_select_index = -1
	var key = search_key_edit.text
	filter_items.clear()
	if !key:
		load_filter = false
		pass
	else:
		load_filter = true
		var index = 0
		for temp in items:
			if temp.find(key) != -1:
				# 说明有
				var temp_dic = {
					"index": index,
					"value":temp
				}
				filter_items.append(temp_dic)
				pass
			index += 1
	load_list()
	pass

# 某个列表被选择之后的回调，这里需要区分是否过滤后的数据，还需要读取过滤后的数据index的
func _on_ItemList_item_selected(index):
	item_list_select_index = index
	


func init_data(call_back):
	var list = DataSource.get_serie_item_list()
	var list_data = []
	
	for item in list:
		list_data.append(item.name)
	
	set_data("选择道具", list_data, call_back, -1)
	

func init_data_items(call_back):
	var list = DataSource.get_serie_item_list()
	var list_data = []
	
	for item in list:
		list_data.append(item.name)
	
	set_data("选择道具", list_data, call_back, -1)


func init_data_sprites(call_back):
	var list = DataSource.get_serie_spirit_list()
	var list_data = []
	
	for item in list:
		list_data.append(item.name)
	
	set_data("选择精灵", list_data, call_back, -1)


func init_dilaog_items(call_back):
	var list = DataSource.get_data_list(DataSource.dialig)
	var list_data = []
	
	for item in list:
		list_data.append(item.name)
	
	set_data("选择对话", list_data, call_back, -1)


func init_drama_items(call_back):
	var list = DataSource.get_data_list(DataSource.drama)
	var list_data = []
	
	for item in list:
		list_data.append(item.name)
	
	set_data("选择剧本", list_data, call_back, -1)
	
